class GlobalMethod{
	/**
	 * 获取两个坐标的旋转角度，
	 */
	public static getRotation(x1:number,y1:number,x2:number,y2:number){		
		const tan = Math.atan((y2-y1+0.1)/(x2-x1+0.1))
		let deg = tan/(Math.PI/180);
		if(x2<x1){
			deg = -(180-deg)
		}
		return deg
	}

	public static getSinCos(x1:number,y1:number,x2:number,y2:number):{sin:number, cos:number}{
		const dis = egret.Point.distance(new egret.Point(x1,y1), new egret.Point(x2,y2))
		const sin = (y2 - y1 + 0.01)/dis
		const cos = (x2 - x1 + 0.01)/dis
		return {
			sin,cos
		}
	}

	public static getDistance(x1:number,y1:number,x2:number,y2:number){
		return egret.Point.distance(new egret.Point(x1,y1), new egret.Point(x2,y2))
	}


    public static random(min:number, max:number):number{
        return Math.random()*(max - min) + min
    }

    /**
	 * 获取一个mc帧动画
	 * @param {stirng} name mc帧动画的文件名
	 * @param {number} playNum 帧动画播放次数 -1为循环播放 默认循环播放
	 */
	public static getMc(name: string, playNum: number = -1): egret.MovieClip {
		let data = RES.getRes(`${name}_json`);
		let txtr = RES.getRes(`${name}_png`);
		let mcFactory: egret.MovieClipDataFactory = new egret.MovieClipDataFactory(data, txtr);
		let mc: egret.MovieClip = new egret.MovieClip(mcFactory.generateMovieClipData());
		// mc.play(playNum);
		return mc;
	}

	 /**
     * 根据name关键字创建一个Bitmap对象。name属性请参考resources/resource.json配置文件的内容。
     * Create a Bitmap object according to name keyword.As for the property of name please refer to the configuration file of resources/resource.json.
     */
    public static createBitmapByName(name: string) {
        let result = new egret.Bitmap();
        let texture: egret.Texture = RES.getRes(name);
        result.texture = texture;
        return result;
    }

    public static loadImage(url:string, listner:any, successFunc:Function, failFunc:Function){		
		let loader = new egret.ImageLoader();
		loader.crossOrigin = "anonymous";// 跨域请求
		loader.load(url)
		loader.once(egret.Event.COMPLETE, successFunc, listner)
		loader.once( egret.IOErrorEvent.IO_ERROR, failFunc, listner);	
	}

    public static httpReq(url:string, param:any, method:'GET'|'POST', listner:any, successFunc:Function, failFunc:Function, contentType:'application/json'|'application/x-www-form-urlencoded' ,token?:string){							
        if(method == 'GET'){
            url = url+'?'+param;
        }
		let req = new egret.HttpRequest();	
		req.open(url, method);	
		req.setRequestHeader("Accept", "*/*");
        if(token) req.setRequestHeader("Authorization", token);
		req.setRequestHeader("Content-Type", contentType);
        if(method == 'GET'){
            req.send();
        }else{
            req.send(param);
        }		
		req.addEventListener(egret.Event.COMPLETE, successFunc,listner);
		if(failFunc!=null){
			req.addEventListener(egret.IOErrorEvent.IO_ERROR, failFunc,listner);
			req.addEventListener(egret.HTTPStatusEvent.CLOSE, failFunc,listner);
		}		
	}	

	public static Loaded(){
        window.parent.postMessage({
            "isLoadComplete": true,
            "url": window.location.href
        }, '*');
	}

	public static  Curry = (fn) => {
 	   //... 表示有1个或者更多的参数
		return function curriedFn(...args){
			//检查传入的参数args的数目是否小于函数fn的参数的个数
			if(fn.length > args.length){
				//如果传入的参数个数不够，返回一个函数，并传入上一次的参数
				return function(){
					//重新返回调用curriedFn
					return curriedFn.apply(
						//concat，连接两个或更多的数组，在args后面追加arguments的内容，arguments表示之前传入的参数
						null,args.concat([].slice.call(arguments))
					);
				}
			}
			//如果参数符合要求，将所有的参数传入fn函数内部
			return fn.apply(null,args);
		}
	}


		/**
	 * 地址栏取参
	 * variable:参数名
	 * 用法 Utils.getQueryVariable('参数名字')
	 */
	public static getQueryVariable = (variable) => {
		var query = window.location.search.substring(1);
		var vars = query.split("&");
		for (var i = 0; i < vars.length; i++) {
			var pair = vars[i].split("=");
			if (pair[0] == variable) { return pair[1]; }
		}
		return (false);
	}

	
	/**
	 * ajax egret.HttpRequest
	 * @{
	 * 	method?:'GET/POST',
	 *  url:'请求地址',
	 * 	data?:{} 参数使用对象格式,
	 * 	headers?:'application/x-www-form-urlencoded||application/json'
	 * }
	 * 用法示例:
	 * Utils.ajax({url:'',data:{}}).then(res=>{}).catch(err=>{})
	 */

	public static ajax(ajaxObj: ajaxObj) {
		/**
		 * Url 默认连接前缀
		 */
		const Url = ''
		/**
		 * 设置headers 
		 */
		ajaxObj.headers = 'application/json'
		return new Promise((resolve: (value: any) => void, reject) => {
			var params = ''
			if (ajaxObj.data) {
				if (ajaxObj.headers) {
					if (ajaxObj.headers == 'application/json') {
						params = JSON.stringify(ajaxObj.data)
					}
				} else {
					for (var key in ajaxObj.data) {
						if (ajaxObj.data.hasOwnProperty(key)) {
							var element = ajaxObj.data[key];
							if (params == '') {
								params += `${key}=${element}`
							} else {
								params += `&${key}=${element}`
							}
						}
					}
				}
			}
			var request = new egret.HttpRequest();
			request.responseType = egret.HttpResponseType.TEXT;
			request.open(ajaxObj.url.substring(0, 4) == 'http' ? ajaxObj.url : Url + ajaxObj.url, ajaxObj.method ? egret.HttpMethod[ajaxObj.method.toLocaleUpperCase()] : egret.HttpMethod.POST);
			request.setRequestHeader("Content-Type", ajaxObj.headers ? ajaxObj.headers : "application/x-www-form-urlencoded");
			request.send(params);
			request.addEventListener(egret.Event.COMPLETE, (event) => {
				var request = <egret.HttpRequest>event.currentTarget;
				var data = JSON.parse(request.response)
				if (data.success) {
					resolve(data.data)
				} else {
					reject(event)
				}
			}, this);
			request.addEventListener(egret.IOErrorEvent.IO_ERROR, (event: egret.IOErrorEvent) => {
				reject(event)
			}, this);
		});
	}	

	public static sleep(ms:number){
		return new Promise((resolve, reject)=>{
			setTimeout(resolve, ms)
		})
	}
}

interface ajaxObj {
	method?: string,
	url: string,
	data?: Object,
	headers?: string
}